<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Review extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('bantuan_helper');
		$this->load->model('Produk_model');
		if(!$this->session->userdata('user_id') && !$this->session->userdata('user_display_name') ):
			redirect('backend');
		endif;
	}

	function index()
	{
		$this->load->library('pagination');
		$offset = $this->uri->segment(4);
		$config['base_url'] = site_url().'/backend/review/index';
		$config['total_rows'] = count($this->Produk_model->getReview('all',FALSE,FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '4';
		
		$data['urut'] = $this->uri->segment(4);
		$data['title'] = 'Review';
		$data['template'] = 'review/index';
		$data['res'] = $this->Produk_model->getReview('all',FALSE,FALSE,$config['per_page'],$offset);
		$this->pagination->initialize($config);
		$this->load->view('backend/index',$data);
	}

	function detail()
	{
		$this->form_validation->set_rules('rat_stat', 'Status','required');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->uri->segment(4);
			$data['title'] = 'Review';
			$data['template'] = 'review/detail';
			$data['row'] = $this->Produk_model->getReview(FALSE,$id,FALSE,FALSE,FALSE);
			$this->load->view('backend/index',$data);
		}
		else
		{
			$id = $this->uri->segment(4);
			$data = array
					(
					'rating'=>$this->input->post('rating'),
					'status'=>$this->input->post('rat_stat')
					);
			$this->Produk_model->editReview($id,$data);
			$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data Berhasil Di update</div>');
			redirect('backend/review/detail/'.$id);
		}	
	}

	function delete($id)
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$this->Produk_model->deleteReview($id);
		$count = count($this->Produk_model->getReview('all',FALSE,FALSE,FALSE,FALSE));
		getPage('review','delete',$uri,$count);
	}
	
	function approve()
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$data = array('status'=>'approve');
		$this->Produk_model->editReview($id,$data);
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil di approve</div>');
		getPage('review','edit',$uri,FALSE);
	}
	
	function unapprove()
	{
		$id = $this->uri->segment(4);
		$uri = $this->uri->segment(5);
		$data = array('status'=>'unapprove');
		$this->Produk_model->editReview($id,$data);
		$this->session->set_flashdata('message_type','<div id="kotak" class="success">Data berhasil di unapprove</div>');
		getPage('review','edit',$uri,FALSE);
	}
	
	function ajax_bulk_action()
	{
		foreach($_POST["review_id"] as $id):
			$this->db->where('review_id',$id);
			$this->db->delete('tb_review');
		endforeach;
	}

}